package com.zr.d_c.serviceimpl;

import java.util.List;

import com.zr.d_c.dao.EvaluateDao;
import com.zr.d_c.dao.OrderDao;
import com.zr.d_c.dao.SellerDao;
import com.zr.d_c.daoimpl.EvaluateDaoImpl;
import com.zr.d_c.daoimpl.OrderDaoImpl;
import com.zr.d_c.daoimpl.SellerDaoImpl;
import com.zr.d_c.model.Evaluate;
import com.zr.d_c.model.Order;
import com.zr.d_c.model.Seller;
import com.zr.d_c.service.OrderService;

import net.sf.json.JSONObject;

public class OrderServiceImpl implements OrderService {

	OrderDao orderdao = new OrderDaoImpl();
	SellerDao sellerdao = new SellerDaoImpl();
	EvaluateDao evaluatedao = new EvaluateDaoImpl();
	@Override
	public JSONObject getCurrentOrderByCid(int cid, int start, int pageSize) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		json.put("total", orderdao.selectCountCurrentOrderByCid(cid));
		List<Order> orders = orderdao.selectCurrentOrderByCid(cid, start, pageSize);
		for (Order order : orders) {
			int sid = order.getSid();
			Seller s = sellerdao.selectSellerBySid(sid);
			String sname = s.getSname();
			String sphone = s.getSphone();
			order.setSname(sname);
			order.setSphone(sphone);
			order.setState("等待接受");
		}
		json.put("rows", orders);
		return json;
	}
	@Override
	public int addOrder(Order o) {
		// TODO Auto-generated method stub
		return orderdao.insertOrder(o);
	}
	@Override
	public JSONObject getHistoryOrderByCid(int cid, int start, int pageSize) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		json.put("total", orderdao.selectCountHistoryOrderByCid(cid));
		List<Order> orders = orderdao.selectHistoryOrderByCid(cid, start, pageSize);
		for (Order order : orders) {
			int sid = order.getSid();
			Seller s = sellerdao.selectSellerBySid(sid);
			String sname = s.getSname();
			String sphone = s.getSphone();
			order.setSname(sname);
			order.setSphone(sphone);
			if(order.getOtemp1().equals("1")){
				order.setState("已撤销");
			}else{
				order.setState("已接受");
			}
			if(order.getOisevaluate()==1){
				Evaluate e = evaluatedao.selectEvaluateByOid(order.getOid());
				order.setEvaluate(e.getEtext());
			}else{
				order.setEvaluate("");
			}
		}
		json.put("rows", orders);
		return json;
	}
	@Override
	public int modifyOisdeleteByOid(int[] oids) {
		// TODO Auto-generated method stub
		int flag = 0;
		for (int j = 0; j < oids.length; j++) {
			if(orderdao.updateOisdeleteByOid(oids[j])>0){
				flag = 1;
			}else{
				flag = 0;
				break;
			}
		}
		
		return flag;
	}
	@Override
	public int modifyOtemp1ByOid(int oid) {
		// TODO Auto-generated method stub
		return orderdao.updateOtemp1(oid);
	}
	@Override
	public int getCountCurrentOrderBySid(int sid) {
		// TODO Auto-generated method stub
		return orderdao.selectOrdersCount(sid);
	}
	@Override
	public JSONObject getHistoryOrderByCid(int cid, int start, int pageSize, String searchname) {
		JSONObject json = new JSONObject();
		json.put("total", orderdao.selectCountHistoryOrderByCid(cid,searchname));
		List<Order> orders = orderdao.selectHistoryOrderByCid(cid, start, pageSize,searchname);
		for (Order order : orders) {
			int sid = order.getSid();
			Seller s = sellerdao.selectSellerBySid(sid);
			String sname = s.getSname();
			String sphone = s.getSphone();
			order.setSname(sname);
			order.setSphone(sphone);
			if(order.getOtemp1().equals("1")){
				order.setState("已撤销");
			}else{
				order.setState("已接受");
			}
			if(order.getOisevaluate()==1){
				Evaluate e = evaluatedao.selectEvaluateByOid(order.getOid());
				order.setEvaluate(e.getEtext());
			}else{
				order.setEvaluate("");
			}
		}
		json.put("rows", orders);
		return json;
	}

}
